package view.component.weapon
{
	import enums.main.UISize;
	
	import feathers.controls.Button;
	import feathers.controls.Screen;
	import feathers.controls.ScrollContainer;
	import feathers.controls.Scroller;
	import feathers.controls.TabBar;
	import feathers.data.ListCollection;
	
	import starling.animation.Tween;
	import starling.core.Starling;
	import starling.events.Event;
	
	import util.FG2FUGenerator;
	
	
	/**
	 * 武器搜索界面基类
	 * @author £iang
	 * 
	 */	
	public class BaseWeaponSearchView extends Screen
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 插入项造成的偏移 */
		public var insertOffset:Number;
		/** 武器类型 */
		public var weaponType:String;
		/** 结果显示界面 */
		public var resultView:SearchResultListView;
		/** 确认按钮 */
		public var ensureBtn:Button;
		/** 搜索区高度 */
		public var searchAreaH:Number;
		/** 滚动条容器 */
		public var mainSC:ScrollContainer;
		
		//-----------------------------
		// properties
		//-----------------------------
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		//-----------------------------
		// methods
		//-----------------------------
		
		/**
		 * 初始化函数 
		 * 
		 */		
		override protected function initialize():void
		{
			mainSC = new ScrollContainer();
			mainSC.scrollerProperties.hasElasticEdges = false;
			mainSC.horizontalScrollPolicy = Scroller.SCROLL_POLICY_OFF;
			mainSC.scrollerProperties.throwEase = "easeOut";
			mainSC.scrollerProperties.interactionMode = 
				Scroller.INTERACTION_MODE_MOUSE;
			mainSC.scrollerProperties.scrollBarDisplayMode = 
				Scroller.SCROLL_BAR_DISPLAY_MODE_NONE;
			mainSC.width = UISize.MAIN_W;
			mainSC.height = UISize.MAIN_H - 
				UISize.MONSTER_TAB_AREA_H * this.dpiScale * 2;
			this.addChild( mainSC );
			
			ensureBtn = FG2FUGenerator.generateRoundRectButton(
				90, 50, this.dpiScale, 12, "small"
			);
			ensureBtn.label = "搜索";
			mainSC.addChild( ensureBtn );
			
			resultView = new SearchResultListView(
				UISize.MAIN_H - UISize.MONSTER_TAB_AREA_H * this.dpiScale * 2 - 
				60 * this.dpiScale, weaponType
			);
			mainSC.addChild( resultView );
			
			addListener();
		}
		
		/**
		 * 绘制函数 
		 * 
		 */		
		override protected function draw():void
		{	
			ensureBtn.y = ( searchAreaH - 60 ) * this.dpiScale;
			ensureBtn.x = ( UISize.MAIN_W - 90 * this.dpiScale ) / 2;
			resultView.y = searchAreaH * this.dpiScale;
			resultView.height = UISize.MAIN_H - UISize.MONSTER_TAB_AREA_H * this.dpiScale * 2 - 
				60 * this.dpiScale;
		}
		
		/**
		 * 播放隐藏动画 
		 * 
		 */		
		public function playHideAnimation():void
		{
			var tween:Tween = new Tween( this, 0.4 );
			
			tween.animate( "alpha", 0);
			Starling.juggler.add( tween );
		}
		
		/**
		 * 批量设置mainTabBar按钮 
		 * @return tabbar按钮
		 * 
		 */		
		public function mainTabFactory():Button
		{
			var button:Button = FG2FUGenerator.generateRoundRectButton(
				UISize.TAB_BUTTON_W3, UISize.TAB_BUTTON_H,
				this.dpiScale, UISize.TAB_BUTTON_H * 0.9, "small"
			);
			
			return button;
		}
		
		/**
		 * 添加侦听 
		 * 
		 */		
		public function addListener():void
		{
			ensureBtn.addEventListener( Event.TRIGGERED, ensureHandler );
		}
		
		public function dispatchSearchEvent():void
		{
			
		}
		
		/**
		 * 根据标签文字列表构造tabbar
		 * @param labelVec 标签文字列表
		 * @return  tabbar
		 * 
		 */		
		public function createTabBar( labelVec:Vector.<String> ):TabBar
		{
			var tabBar:TabBar;
			
			tabBar = new TabBar();
			tabBar.gap = 15 * this.dpiScale;
			tabBar.dataProvider = createTabBarData( labelVec );
			tabBar.tabFactory = mainTabFactory;
			return tabBar;
		}
		
		/**
		 *  构造tabbar标签数据
		 * @param labelVec 标签文字列表
		 * @return  标签数据
		 */		
		public function createTabBarData( labelVec:Vector.<String> ):ListCollection
		{
			var len:int;
			var dataProvider:ListCollection = new ListCollection( [] );
			var obj:Object;
			var i:int;
			
			len = labelVec.length;
			
			for ( i = 0; i < len; i ++ )
			{
				obj = new Object();
				obj.label = labelVec[ i ];
				dataProvider.push( obj );
			}
			
			return dataProvider;
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		/**
		 * 处理点击OK按钮派发的事件 
		 * @param event 点击OK按钮派发的事件 
		 * 
		 */		
		protected function ensureHandler( event:Event ):void
		{
			if ( ensureBtn.label == "搜索" )
			{
				mainSC.scrollToPosition( 0, mainSC.maxVerticalScrollPosition, 0.4 );
				dispatchSearchEvent();
				ensureBtn.label = "返回";
			}
			else
			{
				mainSC.scrollToPosition( 0, 0, 0.4 );
				ensureBtn.label = "搜索";
			}
		}
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		/**
		 * 析构函数 
		 * 
		 */		
		override public function dispose():void
		{
			Starling.juggler.removeTweens( this );
			ensureBtn.dispose();
			FG2FUGenerator.deepDisposeButtonComp( ensureBtn );
			ensureBtn = null;
			super.dispose();
		}
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function BaseWeaponSearchView()
		{
			super();
		}
	}
}